package com.edais.actions;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.edais.service.ChartService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.Message;


@Controller
@RequestMapping("/chart")//接口

public class ChartAction implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -5866641942038011784L;
	private static final Log logger = LogFactory.getLog(ChartAction.class);
	@Autowired
	private ChartService chartService;
	/**
	 * 
	 * @param 查询用户的收益率
	 * @param response
	 */
	@RequestMapping(value = "/getYield")
	//@RequestMapping(value = "/getYield",method=RequestMethod.GET)
	public void getYield(HttpServletRequest request, HttpServletResponse response) {	
		//查询年利率的方法
		try {
			Map<String, Object> parmMap = new HashMap<String, Object>();//返回参数的map
			// 解密请求参数
			Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
	/*		parmMap.put("userId", "28645269");
			parmMap.put("dayNum", "7");*/
			parmMap.put("userId", paramsMap.get("userId"));
			parmMap.put("dayNum", paramsMap.get("dayNum"));
			parmMap.put("flag","1");
			Map<String, Object> jsonMap = new HashMap<String, Object>();//查询结果一个json的字符串
			//查询本金和收益（HX库）
			String result=chartService.getmoneyUserId(parmMap);//Action调ServiceImpl的方法
			//CommonUtil.responseJson(result, response);
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error("select用户收益率错误"+e);
			//后面这些是打印到页面的异常错误提示
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(), Consts.ERROR_CODE, Consts.ErrorMsg.MSG00001,
					Message.CnDescriptionExpression.BUSINESSE_ERROR);
				// 处理返回结果
				CommonUtil.responseJson(result, response);
		}		
	}
	/**
	 * 厚钱宝详情接口数据检查
	 */
	private Map<String,Object> historyProfitCheck(Map<String,String> paramMap) throws Exception{
		Map<String,Object> resMap = new HashMap<String,Object>();		
		String userId = paramMap.get("userId");
		String currentPageStr = paramMap.get(Consts.Page.PAGE_CURRENT_NAME);//当前页
		String pageSizeStr = paramMap.get(Consts.Page.PAGE_SIZE_NAME);//页大小
		Integer current = 1;
		Integer pageSize = 10;//默认显示10条	
		if(StringUtils.isBlank(userId)){
			logger.error("厚钱宝历史收益列表接口数据检查失败：用户id为空" );
			resMap.put("code", Consts.ERROR_CODE);
			resMap.put("message", Message.CnDescriptionExpression.BUSINESSE_ERROR);
			return resMap;
		}
		
		//分页参数
		if (currentPageStr != null && !("".equals(currentPageStr))) {
			current = Integer.parseInt(currentPageStr);
		}
		if (pageSizeStr != null && !("".equals(pageSizeStr))) {
			pageSize = Integer.parseInt(pageSizeStr);
		}
		 
		resMap.put("pageSize", pageSize);
		resMap.put("current", current);
		resMap.put("code", Consts.SUCCESS_CODE);
		resMap.put("message", Consts.SUCCESS_DESCRIBE);
		return resMap;
	}
	/**
	 * 余额宝历史本金列表接口
	 */
	@RequestMapping(value = "/historyAmount")
	public void historyAmount(HttpServletRequest request, HttpServletResponse response){
		// 解密请求参数
		Map<String, String> paramsMap = CommonUtil.decryptParamters(request);
		try {
			// 数据检查
			Map<String,Object> checkMap = historyProfitCheck(paramsMap);
			if (null == checkMap || !Consts.SUCCESS_CODE.equals(checkMap.get("code"))){
				String result = CommonUtil.setResultStringCn(
						new HashMap<String, Object>(), checkMap.get("code").toString(),
						"", checkMap.get("message").toString());
				
				// 处理返回结果
				CommonUtil.responseJson(result, response);
				return;
				
			}
			Map<String, Object> map = new HashMap<String,Object>();
			map.put("pageSize", String.valueOf(checkMap.get(Consts.Page.PAGE_SIZE_NAME)));
			map.put("current", String.valueOf(checkMap.get(Consts.Page.PAGE_CURRENT_NAME)));
			map.put("userId", paramsMap.get("userId"));
			
			
		/*	map.put("current", "1");//页数
			map.put("pageSize", "10");			
			map.put("userId", "28644972");*/
			map.put("flag","2");
			// 查询余额宝历史收益列表
			String result = chartService.queryAmountStatementList(map);
			CommonUtil.responseJson(result, response);
		} catch (Exception e) {
			logger.error("历史本金列表接口异常:" + com.edais.util.CommonUtil.printStackTraceToString(e));
			String result = CommonUtil.setResultStringCn(new HashMap<String, Object>(),
				Consts.ERROR_CODE, "",
				Message.CnDescriptionExpression.BUSINESSE_ERROR);
			CommonUtil.responseJson(result, response);
		}
	}
}
